package com.itheima.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService;
import com.itheima.utils.DateUtils;
import com.itheima.utils.MD5Utils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

@Service(interfaceClass = MemberService.class)
public class MemberServiceImpl implements MemberService{

    @Autowired
    private MemberDao memberDao;
    @Autowired
    private OrderDao orderDao;

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage,pageSize);

        Page<Member> page = memberDao.selectByCondition(queryString);
        List<Member> result = page.getResult();
        long total = page.getTotal();
        return new PageResult(total,result);
    }
    @Override
    public Member findByTelephone(String telephone) {
        return memberDao.findByTelephone(telephone);
    }

    @Override
    public void add(Member member) {
        if (member.getPassword() != null){
            member.setPassword(MD5Utils.md5(member.getPassword()));
        }
        String  fileNumber = member.getFileNumber();
        boolean exist = isFileNumberExist(fileNumber);
        if (exist){
            throw new  RuntimeException("档案号已存在");
        }
        memberDao.add(member);
    }

    @Override
    public void edit(Member member) {
        memberDao.edit(member);
    }
    //根据月份查询会员数量
    public List<Integer> findMemberCountByMonths(List<String> months) {
        List<Integer> memberCount = new ArrayList<>();
        for (String month : months) {
            String date = month+".31";
            Integer count = memberDao.findMemberCountBeforeDate(date);
            memberCount.add(count);
        }
        return memberCount;
    }



    public Result findMemberCount(){
        try {
            List<Map<String, Object>> memberCount = memberDao.findMemberCount();

            List<String> listMonths = new ArrayList<>();
            List<String> listMemberCounts = new ArrayList<>();
            memberCount.forEach(map -> {
                listMonths.add(map.get("months").toString());
                listMemberCounts.add(map.get("memberCount").toString());
            });

            HashMap<String, Object> mapTemp = new HashMap<>();
            mapTemp.put("months",listMonths);
            mapTemp.put("memberCount",listMemberCounts);

            return new Result(true, MessageConstant.GET_SETMEAL_COUNT_REPORT_SUCCESS,mapTemp);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_SETMEAL_COUNT_REPORT_FAIL);
        }
    }
    @Override
    public Member findById(Integer id) {
        Member member = memberDao.findById(id);
        return member;
    }

    @Override
    public void deleteById(Integer id) {
        memberDao.deleteById(id);
    }

    @Override
    public Map<String, List> findNumber() throws Exception {
        // 获取总会员数
        Long memberTotalCount = memberDao.findMemberTotalCount();
        // 获取本月的时间
        String date = DateUtils.parseDate2String(DateUtils.getFirstDay4ThisMonth());
        // 获取本月的预约数
        Integer orderCountAfterDate = orderDao.findOrderCountAfterDate(date);
        // 创建一个map
        Map<String,List> map=new HashMap<>();

        List list1=new ArrayList();
        list1.add("本月预约量");
        list1.add("会员总量");
        List list2=new ArrayList();
        list2.add(orderCountAfterDate);
        list2.add(memberTotalCount);
        map.put("name",list1);
        map.put("number",list2);
        return map;
    }

    public boolean isFileNumberExist(String fileNumber){
        boolean flag = false;
        if (fileNumber != null){
            // 获取所有的会员信息
            List<Member> list = memberDao.findAll();
            // 遍历会员信息
            for (Member member : list) {
                // 获取会员的档案号
                String fileNumber1 = member.getFileNumber();
                if (fileNumber.equals(fileNumber1) || fileNumber == fileNumber1){
                    return flag = true;
                }
            }
        }
        return false;
    }
}
